/*
 * Copyright (C) 2016 Facishare Technology Co., Ltd. All Rights Reserved.
 */
package com.wgj.android.permission;
import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Build;
import android.provider.Settings;
import android.util.Log;
import com.jiaai.xui.view.widget.XAlertMsgDialog;
import com.jiaai.xui.view.widget.XToast;
import com.wgj.android.permission.rom.HuaweiUtils;
import com.wgj.android.permission.rom.MeizuUtils;
import com.wgj.android.permission.rom.MiuiUtils;
import com.wgj.android.permission.rom.QikuUtils;
import com.wgj.android.permission.rom.RomUtils;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
/**
 * Description:
 *
 * @author zhaozp
 * @since 2016-10-17
 */

public class WindowManagerPermission {
    private static final String TAG = "WindowManagerPermission";



    public static boolean checkPermission(Context context) {
        //6.0 版本之后由于 google 增加了对悬浮窗权限的管理，所以方式就统一了
        if (Build.VERSION.SDK_INT < 23) {
            if (RomUtils.checkIsMiuiRom()) {
                return miuiPermissionCheck(context);
            } else if (RomUtils.checkIsMeizuRom()) {
                return meizuPermissionCheck(context);
            } else if (RomUtils.checkIsHuaweiRom()) {
                return huaweiPermissionCheck(context);
            } else if (RomUtils.checkIs360Rom()) {
                return qikuPermissionCheck(context);
            }
        }
        return commonROMPermissionCheck(context);
    }

    private static boolean huaweiPermissionCheck(Context context) {
        return HuaweiUtils.checkFloatWindowPermission(context);
    }

    private static boolean miuiPermissionCheck(Context context) {
        return MiuiUtils.checkFloatWindowPermission(context);
    }

    private static boolean meizuPermissionCheck(Context context) {
        return MeizuUtils.checkFloatWindowPermission(context);
    }

    private static boolean qikuPermissionCheck(Context context) {
        return QikuUtils.checkFloatWindowPermission(context);
    }

    private static boolean commonROMPermissionCheck(Context context) {
        //最新发现魅族6.0的系统这种方式不好用，天杀的，只有你是奇葩，没办法，单独适配一下
        if (RomUtils.checkIsMeizuRom()) {
            return meizuPermissionCheck(context);
        } else {
            Boolean result = true;
            if (Build.VERSION.SDK_INT >= 23) {
                try {
                    Class clazz = Settings.class;
                    Method canDrawOverlays = clazz.getDeclaredMethod("canDrawOverlays", Context.class);
                    result = (Boolean) canDrawOverlays.invoke(null, context);
                } catch (Exception e) {
                    Log.e(TAG, Log.getStackTraceString(e));
                }
            }
            return result;
        }
    }

    public static void applyPermission(Context context) {
        if (Build.VERSION.SDK_INT < 23) {
            if (RomUtils.checkIsMiuiRom()) {
                miuiROMPermissionApply(context);
            } else if (RomUtils.checkIsMeizuRom()) {
                meizuROMPermissionApply(context);
            } else if (RomUtils.checkIsHuaweiRom()) {
                huaweiROMPermissionApply(context);
            } else if (RomUtils.checkIs360Rom()) {
                ROM360PermissionApply(context);
            }
        }
        commonROMPermissionApply(context);
    }

    private static void ROM360PermissionApply(final Context context) {
        showMsgDialog(context,new XAlertMsgDialog.OnCloseListener() {
            @Override
            public void onClick(Dialog dialog, boolean b) {
                if(b){
                    QikuUtils.applyPermission(context);
                }else {
                    XToast.error("没有权限请先获取权限");
                }
            }
        });

    }

    private static void huaweiROMPermissionApply(final Context context) {
        showMsgDialog(context,new XAlertMsgDialog.OnCloseListener() {
            @Override
            public void onClick(Dialog dialog, boolean b) {
                if(b){
                    HuaweiUtils.applyPermission(context);
                }else {
                    XToast.error("没有权限请先获取权限");
                }
            }
        });
    }

    private static void meizuROMPermissionApply(final Context context) {
        showMsgDialog(context,new XAlertMsgDialog.OnCloseListener() {
            @Override
            public void onClick(Dialog dialog, boolean b) {
                if(b){
                    MeizuUtils.applyPermission(context);
                }else {
                    XToast.error("没有权限请先获取权限");
                }
            }
        });

    }

    private static void miuiROMPermissionApply(final Context context) {
        showMsgDialog(context,new XAlertMsgDialog.OnCloseListener() {
            @Override
            public void onClick(Dialog dialog, boolean b) {
                if(b){
                    MiuiUtils.applyMiuiPermission(context);
                }else {
                    XToast.error("没有权限请先获取权限");
                }
            }
        });


    }

    /**
     * 通用 rom 权限申请
     */
    private static void commonROMPermissionApply(final Context context) {
        //这里也一样，魅族系统需要单独适配
        if (RomUtils.checkIsMeizuRom()) {
            meizuROMPermissionApply(context);
        } else {
            if (Build.VERSION.SDK_INT >= 23) {
                showMsgDialog(context,new XAlertMsgDialog.OnCloseListener() {
                    @Override
                    public void onClick(Dialog dialog, boolean b) {
                        if(b){
                            try {
                                Class clazz = Settings.class;
                                Field field = clazz.getDeclaredField("ACTION_MANAGE_OVERLAY_PERMISSION");
                                Intent intent = new Intent(field.get(null).toString());
                                intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                                intent.setData(Uri.parse("package:" + context.getPackageName()));
                                context.startActivity(intent);
                            } catch (Exception e) {
                                Log.e(TAG, Log.getStackTraceString(e));
                            }
                        }else {
                            XToast.error("没有权限请先获取权限");
                        }
                    }
                });

            }
        }
    }


    private static  void  showMsgDialog(Context context,XAlertMsgDialog.OnCloseListener closeListener){
        new XAlertMsgDialog(context,"需要获取悬浮窗权限").setPositiveButton("现在开启").setNegativeButton("放弃").setListener(closeListener).show();
    }





}
